package org.budo.graph.service.util;

import java.util.List;

import org.budo.support.spring.bean.util.PropertyValueUtil;
import org.budo.support.spring.util.SpringUtil;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.ApplicationContext;

import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.JedisPool;

/**
 * @author lmw
 */
@Slf4j
public class RedisPoolUtil {
    public static String writeRedisPoolResponse(ApplicationContext applicationContext, String url) {
        log.info("#14 writeRedisPoolResponse, applicationContext=" + applicationContext + ", url=" + url);

        String response = "";

        List<String> beanNames = SpringUtil.getBeanNamesForType(JedisPool.class);
        for (String name : beanNames) {
            BeanDefinition beanDefinition = SpringUtil.getBeanDefinition(applicationContext, name);
            String beanClassName = beanDefinition.getBeanClassName();

            response += "<br/>name=" + name + "<br/>";
            response += "type=" + beanClassName + "<br/>";
            response += "resource=" + beanDefinition.getResourceDescription() + "<br/>";

            if ("org.budo.jedis.pool.factory.BudoJedisPoolFactory".equals(beanClassName)) {
                Object host = PropertyValueUtil.get(beanDefinition.getPropertyValues(), "host");
                response += "host=" + host + "<br/>";

                Object port = PropertyValueUtil.get(beanDefinition.getPropertyValues(), "port");
                response += "port=" + port + "<br/>";

                Object database = PropertyValueUtil.get(beanDefinition.getPropertyValues(), "database");
                response += "database=" + database + "<br/>";

                Object timeout = PropertyValueUtil.get(beanDefinition.getPropertyValues(), "timeout");
                response += "timeout=" + timeout + "<br/>";
                continue;
            }
        }

        return response;
    }
}